Spring+SpringMVC+Thymeleaf 示例(基于 Java 配置)

您所在的位置:网站首页 spring mvc thymeleaf根据名称获取页面 Spring+SpringMVC+Thymeleaf 示例(基于 Java 配置)

Spring+SpringMVC+Thymeleaf 示例(基于 Java 配置)

2024-06-01 08:57| 来源: 网络整理| 查看: 265

Spring+SpringMVC+Thymeleaf 示例(基于 Java 配置) 基于 Spring Tool Suit 搭建 Spring + Spring MVC + Thymeleaf(基于 Java 配置) 参考资料 官方文档 (5.2.8) Spring Tool 历史版本下载 Spring Tool 4 安装 Thymeleaf 3.0 插件 SpringMVC快速使用——基于XML配置和Servlet3.0 1 环境准备 spring tool suit 4.7.1.RELEASE thymeleaf eclipse 插件 jdk 1.8 tomcat 8.5 apache maven 3.6 2 使用 maven 模板构建 war 工程

参见《Spring+SpringMVC+Thymeleaf 示例(基于 XML 配置)》博客的第 2 部分。

3 配置 Spring MVC 3.1 引入 Spring jar UTF-8 1.8 1.8 5.3.8 1.9.6 2.12.7 org.springframework spring-beans ${spring.version} org.springframework spring-core ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-context-support ${spring.version} org.springframework spring-expression ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-aop ${spring.version} org.springframework spring-aspects ${spring.version} org.aspectj aspectjweaver ${aspectj.version} compile com.fasterxml.jackson.core jackson-core 2.12.7 com.fasterxml.jackson.core jackson-databind 2.12.7 com.fasterxml.jackson.core jackson-annotations 2.12.7 javax.servlet javax.servlet-api 3.1.0 provided 3.2 MVC 配置 3.2.1 开启 MVC 特性 & 组件扫描 package com.black.app.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** *

Web MVC 定制化配置

*

@EnableWebMvc 注解开启 Spring Web MVC 特性

*

@ComponentScan 扫描 @Component Bean Definition

* @author limo * @since 1.0.0 */ @Configuration @EnableWebMvc @ComponentScan(basePackages = {"com.black.app.controller"}) public class WebMvcConfig implements WebMvcConfigurer { // 可实现 WebMvcConfigurer 接口方法进行定制化 MVC 配置 }

@EnableWebMvc :开启 MVC 特性

@ComponentScan:扫描 @Component 注解(或派生注解)的 Bean,这里主要是扫描com.black.app.controller 包下的 Controller

3.2.2 配置 WebApplicationInitializer

这里通过继承 WebApplicationInitializer 接口的实现类AbstractAnnotationConfigDispatcherServletInitializer ,将 WebMvcConfig 中的 Bean 定义注册到 WebApplicationContext 中。

继承 AbstractAnnotationConfigDispatcherServletInitializer 配置 DispatcherServlet & 添加 Servlet 映射 :

package com.black.app.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; /** * @description Servlet 3.0标准提供了 ServletContainerInitializer#onStartup 能够编程式配置 Servlet等信息; * Spring 提供 SpringServletContainerInitializer 类会在容器启动时加载此实现类中的 ServletConfig 配置。 * @author limo * @since 1.0.0 */ public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { // Root WebApplicationContext Java 配置类 return null; } @Override protected Class[] getServletConfigClasses() { // 1. Servlet WebApplicationContext Java 配置类 return new Class[]{WebMvcConfig.class}; } @Override protected String[] getServletMappings() { // 2. 配置 DispatcherServlet 映射 String[] servletMappings = new String[] {"/"}; return servletMappings; } }

在 Servlet 容器启动时,会调用 SpringServletContainerInitializer#onStartup() 方法;

SpringServletContainerInitializer 会调用 WebApplicationInitializer 类的 onStartup进行编程式配置 Servlet。

将/src/main/webapp/index.jsp 重命名为 index.html。

3.3 新建 Controller

EchoController

package com.black.app.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.black.app.response.Response; /** * @description echo 服务 * @author limo * @date 2023-7-22 * @since 1.0.0 */ @Controller @RequestMapping("/echo") public class EchoController { /** * @description 将前端访问的 content,拼接“服务器收到:”后返回 echo.html 页面显示 * @param content * @return */ @RequestMapping(method = RequestMethod.GET, path = "/{content}") public ModelAndView echo(@PathVariable("content") String content) { ModelAndView echoView = new ModelAndView(); String message = "服务器收到:" + content; echoView.setViewName("echo"); echoView.addObject("content", message); return echoView; } /** * @description 返回 json view * @param request * @return */ @RequestMapping(path = "/body", method = RequestMethod.POST) @ResponseBody public Response body(@RequestBody Object request){ return Response.success(request); } }

UserController

package com.black.app.controller; import java.util.Date; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.black.app.response.Response; import com.fasterxml.jackson.annotation.JsonFormat; /** * @description user controller * @author limo * */ @Controller public class UserController { /** * @description 获取用户详情,返回 detail.html页面展示 * @return */ @RequestMapping(path = "/detail", method = RequestMethod.GET) public ModelAndView detail() { User user = new User(); user.setName("black"); user.setPhone("182XXXXXXXX"); user.setAge(18); user.setAddress("xx市xx区xx小区"); user.setBirthday(new Date()); ModelAndView detailView = new ModelAndView(); detailView.setViewName("detail"); detailView.addObject("user", user); return detailView; } /** * @description 注册用户,并返回json格式的成功响应 * @param user * @return */ @RequestMapping(path = "/register", method = RequestMethod.POST) @ResponseBody public Response register(@RequestBody User user) { // ModelAndView detailView = new ModelAndView(); // detailView.setViewName("detail"); // detailView.addObject("user", user); return Response.success(user); } } /** * @description 用户信息 * @author limo * */ class User { private String name; private Integer age; private String phone; private String address; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }

Response

package com.black.app.response; /** * 响应实体 * @author limo * * @param */ public class Response { private String code; private String message; private T data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } public static Response success(T data){ Response response = new Response(); response.setCode("000000"); response.setData(data); response.setMessage("成功"); return response; } } 3.4 集成 Thymeleaf 3.4.1 引入 Thymeleaf 相关 jar

pom.xml 引入 thymeleaf jar 包:

org.thymeleaf thymeleaf-spring5 3.0.15.RELEASE org.thymeleaf thymeleaf 3.0.15.RELEASE ognl ognl 3.1.26 org.slf4j slf4j-api 1.7.25 org.attoparser attoparser 2.0.5.RELEASE org.unbescape unbescape 1.1.6.RELEASE 3.4.2 配置 Thymeleaf 相关的 Bean

依次配置:

ITemplateResolver ISpringTemplateEngine ViewResolver package com.black.app.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.thymeleaf.spring5.ISpringTemplateEngine; import org.thymeleaf.spring5.SpringTemplateEngine; import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring5.view.ThymeleafViewResolver; import org.thymeleaf.templatemode.TemplateMode; import org.thymeleaf.templateresolver.ITemplateResolver; /** * @description 集成 Thymeleaf * @author limo * @date 2023-7-22 * @since 1.0.0 */ @Configuration public class ThymeleafConfig { // 1. 配置 templateResolver @Bean public ITemplateResolver templateResolver() { SpringResourceTemplateResolver templageResolver = new SpringResourceTemplateResolver(); templageResolver.setPrefix("/WEB-INF/templates/"); templageResolver.setSuffix(".html"); templageResolver.setTemplateMode(TemplateMode.HTML); templageResolver.setCacheable(true); templageResolver.setCharacterEncoding("utf-8"); return templageResolver; } // 2. 配置 templateEngine @Bean public ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver); templateEngine.setEnableSpringELCompiler(true); return templateEngine; } // 3. 配置 ThymeleafViewResolver @Bean public ViewResolver viewResolver(ISpringTemplateEngine templateEngine) { ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver(); thymeleafViewResolver.setCharacterEncoding("utf-8"); thymeleafViewResolver.setTemplateEngine(templateEngine); return thymeleafViewResolver; } } 3.4.3 调整 com.black.app.config.WebMvcConfig

@Import 导入 ThymeleafConfig 中相关的 Bean 到 WebMvcConfig 类中:

package com.black.app.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** *

Web MVC 定制化配置

*

@EnableWebMvc 注解开启 Spring Web MVC 特性

*

@ComponentScan 扫描 @Component Bean Definition

* @author limo * @since 1.0.0 */ @Configuration @EnableWebMvc @ComponentScan(basePackages = {"com.black.app.controller"}) @Import({ThymeleafConfig.class}) public class WebMvcConfig implements WebMvcConfigurer { } 3.4.4 添加模板 echo.html & detail.html

在 /src/main/webapp/WEB-INF/templates 添加:

echo.html echo page

detail.html detail page

至此 Thymleaf 集成完成。

4 部署到 tomcat 4.1 使用 Maven build 打出 war 包 4.2 将 war 包放于 apache-tomcat-8.5.61\webapps 下 4.3 调整 apache-tomcat-8.5.61\conf\server.xml ... 4.4 启动 tomcat

双击 apache-tomcat-8.5.61\bin\startup.bat

... org.springframework.web.servlet.DispatcherServlet - Detected org.springframework.web.servlet.support.SessionFlashMapManager@7d63fbe4 20:09:30.975 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/spring.liveBeansView.mbeanDomain] 20:09:30.976 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/spring.liveBeansView.mbeanDomain] not found - trying original name [spring.liveBeansView.mbeanDomain]. javax.naming.NameNotFoundException: 名称[spring.liveBeansView.mbeanDomain]未在此上下文中绑定。找不到[spring.liveBeansView.mbeanDomain]。 20:09:30.976 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [spring.liveBeansView.mbeanDomain] 20:09:30.977 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [spring.liveBeansView.mbeanDomain] threw NamingException with message: 名称[spring.liveBeansView.mbeanDomain]未在此上下文中绑定。找不到[spring.liveBeansView.mbeanDomain]。. Returning null. 20:09:30.980 [localhost-startStop-1] DEBUG org.springframework.web.servlet.DispatcherServlet - enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data 20:09:30.980 [localhost-startStop-1] INFO org.springframework.web.servlet.DispatcherServlet - Completed initialization in 1388 ms 23-Jul-2023 20:09:30.996 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [C:\work\soft\apache-tomcat-8.5.61\webapps\docs] 23-Jul-2023 20:09:31.013 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Web应用程序目录[C:\work\soft\apache-tomcat-8.5.61\webapps\docs]的部署已在[16]毫秒内完成 ... 23-Jul-2023 20:09:31.345 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-nio-8080"] 23-Jul-2023 20:09:31.354 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 6736 ms

如果 CMD 窗口打印日志乱码,可编辑 apache-tomcat-8.5.61\conf\logging.properties 文件的 “java.util.logging.ConsoleHandler.encoding” 设置为 GBK

5 测试验证 5.1 首页

浏览器访问 : http://localhost:8080/api

image

5.2 echo 页面(如果配置了 Thymeleaf)

浏览器访问 : http://localhost:8080/api/echo/black

image

5.3 user 页面(如果配置了 Thymeleaf)

浏览器访问 : http://localhost:8080/api/detail

image

补充资料 1 基于 Java 类的 IOC 容器配置

官方文档:https://docs.spring.io/spring-framework/docs/4.3.9.RELEASE/spring-framework-reference/htmlsingle/#beans-java

基本概念 @Configuration 注解(标注在类上,表示该类是 BeanDefinition 来源,类似于 applicationContext.xml) @Bean注解(标注在方法上,表示该方法将实例化、配置、初始化一个对象交给 Spring IOC 容器管理)

@Configuration 标注的类也是一个 Bean Definition,也会交给 Spring IOC 容器管理。

@Bean注解通常和 @Configuration 注解搭配使用:

@Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } }

注意:@Bean 也可以和 @Component注解搭配使用,被称为轻量级 @Bean 方法;同一个 @Component标注的类中的,多个@Bean 无法进行依赖注入。

使用 AnnotationConfigApplicationContext 实例化 Spring IOC 容器

AnnotationConfigApplicationContext 支持的配置类包括:

@Component 标注的类 @Configuration 标注的类 jsr330 标准相关注解标注的类 public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); } public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class, OtherConfig.class); ctx.register(AdditionalConfig.class); ctx.refresh(); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); } @Configuration 使用

注入内部Bean 依赖

@Configuration public class AppConfig { // 默认使用方法名作为 Bean 的名字 @Bean public Foo foo() { // 通过本地方法调用注入 Bean return new Foo(bar()); } @Bean public Bar bar() { return new Bar(); } } 组合基于 Java 配置 1 使用 @Import注解

@Import注解类似 XML 中的 标签

Spring 提供的 @Enable* 注解均采用了 @Import 方式

@Configuration public class ConfigA { @Bean public A a() { return new A(); } } // 使用 @Import 将 ConfigA中的 Bean Definition 导入到 ConfigB中 @Configuration @Import(ConfigA.class) public class ConfigB { @Bean public B b() { return new B(); } }

@Import注解支持导入的类:

@Configuration ImportSelector ImportBeanDefinitionRegistrar @Component 类(从 4.2 开始)

复杂导入:

@Configuration public class ServiceConfig { @Autowired private RepositoryConfig repositoryConfig; @Bean public TransferService transferService() { return new TransferServiceImpl(repositoryConfig.accountRepository()); } } @Configuration public interface RepositoryConfig { @Bean AccountRepository accountRepository(); } @Configuration public class DefaultRepositoryConfig implements RepositoryConfig { @Bean public AccountRepository accountRepository() { return new JdbcAccountRepository(...); } } @Configuration @Import({ServiceConfig.class, DefaultRepositoryConfig.class}) // 导入具体的 DefaultRepositoryConfig! public class SystemTestConfig { @Bean public DataSource dataSource() { // return DataSource } } public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class); TransferService transferService = ctx.getBean(TransferService.class); transferService.transfer(100.00, "A123", "C456"); } 2 使用@Conditional 注解

@Profile 注解基于 @Conditional注解实现的。

在 @Bean 被注册前,@Conditional指示使用org.springframework.context.annotation.Condition的具体实现进行判定结果是否需要注册。

本文来自博客园,作者:不安分的黑娃,转载请注明原文链接:https://www.cnblogs.com/lihw-study/p/15037362.html

posted on 2021-07-20 22:18  不安分的黑娃  阅读(138)  评论(0)  编辑  收藏  举报


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3